// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  /* 页面的初始数据 */
  data: {
    info: 'hello world',
    imageUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
    randomNum1: Math.random() * 10,
    randomNum2: Math.random().toFixed(2),
    count: 2,
    msg: '你好',
    type: 1,
    flag: false,
    arr: ['苹果', '香蕉', '榴莲'],
    userList: [
      {id: 1, name: '李明'},
      {id: 2, name: '李华'},
      {id: 3, name: '荔枝'},
    ] 
  },
  // 定义按钮的触发事件
  handlerTap(e) {
    console.log(e)
  },
  // 定义+1按钮的触发事件
  changeCount(e) {
    this.setData({
      count: this.data.count + 1
    })
  },
  // 定义+2按钮的触发事件
  addTwo(e) {
    console.log(e.target.dataset.val)
    this.setData({
      count: this.data.count + e.target.dataset.val
    })
  },
  // 定义input输入框事件
  handlerInput(e) {
    console.log(e.detail.value)
    this.setData({
      msg: e.detail.value
    })
  },

  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
})
